package com.masterclient.app.modules.goods.newpage.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.eoner.baselibrary.bean.goods.CommodityActivityMessage;
import com.eoner.baselibrary.bean.goods.CommodityDetailData;
import com.masterclient.app.modules.timelimited.TimeLimitedBuyActivity;
import com.eoner.baselibrary.utils.TimeUtil;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class NewCommodityDetailTimeLimitAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    Activity mContext;

    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private TimeDownFinishListener mTimeDownFinishListener;
    private int mCount = 0;
    CommodityDetailData data;

    public interface TimeDownFinishListener{
        void onTimeDownFinish();
    }


    public NewCommodityDetailTimeLimitAdapter(Activity context, LayoutHelper layoutHelper, int count,TimeDownFinishListener mTimeDownFinishListener) {
        this(context, layoutHelper, count, null,null,null);
    }

    public NewCommodityDetailTimeLimitAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams,
                                              CommodityDetailData data,TimeDownFinishListener mTimeDownFinishListener) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.data = data;
        this.mTimeDownFinishListener = mTimeDownFinishListener;
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }
    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }
    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if(mLayoutParams!=null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        View time_activity_layout = holder.itemView.findViewById(R.id.time_activity_layout);
        TextView remain_time = holder.itemView.findViewById(R.id.remain_time);
        TextView start_or_over_time = holder.itemView.findViewById(R.id.start_or_over_time);


        time_activity_layout.setOnClickListener(v -> {
            Intent intent = new Intent(mContext,TimeLimitedBuyActivity.class);
            intent.putExtra("intentSource","商品详情");
            mContext.startActivity(intent);
        });

        CommodityActivityMessage commodityActivityMessage = data.getSingle_promotion();
        if(commodityActivityMessage == null){
            time_activity_layout.setVisibility(View.GONE);
        }else{
            time_activity_layout.setVisibility(View.VISIBLE);

            if (TimeUtil.isBeforeNow(commodityActivityMessage.getStart_at()) && !TimeUtil.isBeforeNow(commodityActivityMessage.getEnd_at())) {
                //抢购中
                long overTime = System.currentTimeMillis() + TimeUtil.timeRemain2(commodityActivityMessage.getEnd_at());
                start_or_over_time.setText(TimeUtil.formatTime(overTime, "MM月dd日 HH:mm 结束"));

                long amount = (TimeUtil.timeRemain2(commodityActivityMessage.getEnd_at()) / 1000);
                Observable.interval(0, 1, TimeUnit.SECONDS, Schedulers.computation())
                        .take(amount).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Long aLong) {
                        String time = TimeUtil.getShowTimeStr((amount - aLong) * 1000);
                        remain_time.setText(time);
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                        time_activity_layout.setVisibility(View.GONE);
                        mTimeDownFinishListener.onTimeDownFinish();
                    }

                });
            }else if (!TimeUtil.isBeforeNow(commodityActivityMessage.getStart_at())) {
                //限时购未开始
                long startTime = System.currentTimeMillis() + TimeUtil.timeRemain2(commodityActivityMessage.getStart_at());
                start_or_over_time.setText(TimeUtil.formatTime(startTime, "MM月dd日 HH:mm 开售"));

                long amount = (TimeUtil.timeRemain2(commodityActivityMessage.getStart_at()) / 1000);
                Observable.interval(0, 1, TimeUnit.SECONDS, Schedulers.computation())
                        .take(amount).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Long aLong) {
                        String time = TimeUtil.getShowTimeStr((amount - aLong) * 1000);
                        remain_time.setText(time);
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                        time_activity_layout.setVisibility(View.GONE);
                        mTimeDownFinishListener.onTimeDownFinish();
                    }

                });

            }else{
                time_activity_layout.setVisibility(View.GONE);
            }

        }


    }


    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }
}
